package net.ghy.statistics.other;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import dao.hb.BxDetailrecord;
import dao.hb.BxMidle;
import dao.hb.UUser;

import net.bat.db.BaseDAO;
import net.bat.db.BaseDAOImpl;
import net.ghy.statistics.utils.EUtils;

public class OtherData {
	static private BaseDAO dao;
	public BaseDAO getDao() {
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		return dao;
	}

	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}
	//分用户统计
	public List<Map> userbxData(String type,String begin,String end){
		BxMidle usertj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		List user_bxtj = new ArrayList<Map>();
		String hql = "select distinct b.uid from BxMidle b where b.bxtype='"+type+"'" + " and ( b.dtLook >= '" +begin + "' and b.dtLook <='" + end +"')";;
		List uids = getDao().find(hql);
		if(uids!=null && uids.size()>0){
			for(int i = 0 ;i<uids.size();i++){
				String uid = ((Integer) uids.get(i)).toString();
				user_bxtj.add(getTjUser(uid,type,begin,end));
			}
			user_bxtj.add(getTjUser("合计",type,begin,end));
		}		
		return user_bxtj;
       
	}

	public Map<String,BxMidle> getTjUser(String uid,String type,String begin,String end){
		Map<String, BxMidle> userBx = new HashMap<String, BxMidle>();
		BxMidle usertj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		double cpayone = usertj.getPayone(),cpaytwo = usertj.getPaytwo(),cpayall = usertj.getPayall(),ctotal=usertj.getBxSum();
		String hql = "from BxMidle b where  b.bxtype='" + type + "'"+ " and ( b.dtLook >= '" +begin + "' and b.dtLook <='" + end +"') order by b.dtLook";
		String uname = "合计";
		if(!uid.equals("合计")){
			hql = "from BxMidle b where  b.bxtype='" + type + "'"
					+ "  and b.uid=" + uid+ " and ( b.dtLook >= '" +begin + "' and b.dtLook <='" + end +"') order by b.dtLook";
			uname=EUtils.getUserbyid(uid).getName();
		}
		List ubx = getDao().find(hql);
		for (int i = 0; i < ubx.size(); i++) {
			BxMidle b = (BxMidle) ubx.get(i);
			if(b.getPayone()==null)
				b.setPayone(0.0);
			if(b.getPaytwo()==null)
				b.setPaytwo(0.0);
			if(b.getPayall()==null)
				b.setPayall(0.0);
			if(b.getBxSum()==null)
				b.setBxSum(0.0);
			cpayone += Math.round(b.getPayone()*100);
			cpaytwo += Math.round(b.getPaytwo()*100);
			cpayall += Math.round(b.getPayall()*100);
			ctotal += Math.round(b.getBxSum()*100);
		}
		cpayone +=Math.round(usertj.getPayone()*100);
		cpaytwo +=Math.round(usertj.getPaytwo()*100);
		cpayall +=Math.round(usertj.getPayall()*100);
		ctotal +=Math.round(usertj.getBxSum()*100);
		usertj.setPayone(cpayone/100);
		usertj.setPaytwo(cpaytwo/100);
		usertj.setPayall(cpayall/100);
		usertj.setBxSum(ctotal/100);
		userBx.put(uname,usertj);
		return userBx;
	}

	public Map<String,BxMidle> getTjUser(String hql){
		
		Map<String, BxMidle> userBx = new HashMap<String, BxMidle>();
		BxMidle usertj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		double cpayone = usertj.getPayone(),cpaytwo = usertj.getPaytwo(),cpayall = usertj.getPayall(),ctotal=usertj.getBxSum();
		List ubx = getDao().find(hql);
		for (int i = 0; i < ubx.size(); i++) {
			BxMidle b = (BxMidle) ubx.get(i);
			if(b.getPayone()==null)
				b.setPayone(0.0);
			if(b.getPaytwo()==null)
				b.setPaytwo(0.0);
			if(b.getPayall()==null)
				b.setPayall(0.0);
			if(b.getBxSum()==null)
				b.setBxSum(0.0);
			cpayone += Math.round(b.getPayone()*100);
			cpaytwo += Math.round(b.getPaytwo()*100);
			cpayall += Math.round(b.getPayall()*100);
			ctotal += Math.round(b.getBxSum()*100);
		}
		cpayone +=Math.round(usertj.getPayone()*100);
		cpaytwo +=Math.round(usertj.getPaytwo()*100);
		cpayall +=Math.round(usertj.getPayall()*100);
		ctotal +=Math.round(usertj.getBxSum()*100);
		usertj.setPayone(cpayone/100);
		usertj.setPaytwo(cpaytwo/100);
		usertj.setPayall(cpayall/100);
		usertj.setBxSum(ctotal/100);
		userBx.put("合计",usertj);
		return userBx;
	}
	// single normal user statistics
	public Map<String,Map<String, BxMidle>> singleuserbxData(String uid, String type, String begin,String end, List wkstates) {
		BxMidle usertj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		Map user_bxtj = new HashMap<String,Map<String, BxMidle>>();
		for (int i = 0; i < wkstates.size(); i++) {
			String hql = "from BxMidle b where b.bxtype='" + type + "'"
					+ " and b.wkState='" + wkstates.get(i) + "'  and b.uid=" + uid
					+ " and ( b.dtLook >= '" + begin + "' and b.dtLook <='"
					+ end + "') order by b.dtLook ";
			List bxmids = getDao().find(hql);
			Map<String, BxMidle> userBx = getTjUser(hql);
			BxMidle object = userBx.get("合计");
			Map<String, BxMidle> rep = new TreeMap<String, BxMidle>();
			if (bxmids != null && bxmids.size() > 0) {
				for (int j = 0; j < bxmids.size(); j++) {
					BxMidle b = (BxMidle) bxmids.get(j);
					rep.put(j + "", b);
				}
				rep.put("合计", object);
				if(wkstates.get(i).equals("在职")&&begin.contains("01-01")){
					BxMidle baogf = new BxMidle();
					UUser u = EUtils.getUserbyid(uid);
					baogf.setPayone(u.getBaogf());
					baogf.setBxSum(object.getBxSum()-u.getBaogf());
					rep.put("实扣包干费", baogf);
					
				}
				user_bxtj.put( wkstates.get(i),rep);
			}

			
		}// end for
		return user_bxtj;
	}
	
	public List getUserWkState(String uid,String type,String begin,String end){
    	String hql = "select distinct wkState from BxMidle b where b.bxtype='"+type+"'" +" and b.uid="+uid + " and ( b.dtLook >= '" +begin + "' and b.dtLook <='" + end +"') order by b.dtLook " ;
    	List states = getDao().find(hql);
    	return states;
	}
    
}
